package com.powernode.springmvc.controller.springmvc.controller;

import com.powernode.springmvc.controller.springmvc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class UserController {

    @RequestMapping("/")
    public String toRegister(){
        //返回一个逻辑视图
        return "register";
    }

//    @RequestMapping(value = "/user/reg",method = RequestMethod.POST)
//    @PostMapping("/user/reg")
//    public String register(HttpServletRequest request, HttpServletResponse response, HttpSession session){
//        //HttpServletRequest HttpServletResponse HttpSession都是属于原生的Servlet API
//        System.out.println(request);
//        System.out.println(response);
//        System.out.println(session);
//        //获取请求提交的数据
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        String sex = request.getParameter("sex");
//        String[] interest = request.getParameterValues("interest");//多个兴趣爱好
//        String intro = request.getParameter("introduce");
//        System.out.println(username);
//        System.out.println(password);
//        System.out.println(sex);
//        System.out.println(Arrays.toString(interest));
//        System.out.println(intro);
//        return "ok";
//
//    }

//    @PostMapping(value = "/user/reg")
//    public String Register(
//            //name是value的别名
//            @RequestParam(value = "username")  //将请求参数与方法上的形参映射
//            String username,
//            @RequestParam(name = "password")
//            String password,
//            @RequestParam("sex")
//            Integer sex,
//            @RequestParam("interest")
//            String[] interest,
//            @RequestParam("introduce")
//            String intro,
//            @RequestParam(value = "age", required = false,defaultValue = "20")
//            //意味着没有前端表单没有给这个age属性也不会报错，
//            //并且默认age=20，而如果自己给了age值，就按照表单提交的数据
//            Integer age){
//        System.out.println(username);
//        System.out.println(password);
//        System.out.println(sex);
//        System.out.println(interest);
//        System.out.println(intro);
//        return "ok";
//    }

//    @PostMapping("/user/reg")
//    public String register(String username,String password,Integer sex,String interest,String introduce,
//                           @RequestParam(defaultValue = "20")
//                           Integer age){
//        System.out.println(username);
//        System.out.println(password);
//        System.out.println(sex);
//        System.out.println(interest);
//        //draw,tea,shopping
//        //如果用String接收多个参数，会自动拼接成字符串
//        System.out.println(introduce);
//        System.out.println(age);
//        return "ok";
//    }

    //@PostMapping("/user/reg")
    @RequestMapping("/user/reg")
    public String register(User user,
                           @RequestHeader(value = "Referer",required = false,defaultValue = "")
                           String referer,
                           @RequestHeader(value = "Host")
                           String host,
                           @CookieValue(value = "id",required = false,defaultValue = "")
                           String id){
        //请求参数是否可以赋值到JavaBean对应的属性上，不是取决于属性名，而是setter方法名
        System.out.println(user);
        System.out.println(referer);//http://192.168.108.1:8080/springmvc03/
        System.out.println(host);
        System.out.println("客户端提交过来的cookie值："+id);
        //客户端提交过来的cookie值：123456789
        return "ok";
    }
}
